package com.fz.security.utils;

import com.fz.common.Global;
import com.fz.entity.member.Member;
import com.fz.utils.JacksonUtils;
import com.fz.utils.RedisUtil;
import com.fz.utils.SpringContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class SecurityUtils {


    protected final Logger logger = LoggerFactory.getLogger(SecurityUtils.class.getSimpleName());


    /**
     * @方法描述: 获取当前用户的token
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/29
     */
    public static String getToken(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader(Global.AUTHORIZE_TOKEN_CLIENT);
        return token;
    }


    /**
     * @方法描述: 获取当前用户的基本信息
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/29
     */
    public static Member getCurrentUser(){
        RedisUtil redisUtil = SpringContextUtil.getBean(RedisUtil.class);
        Member u = null;
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader(Global.AUTHORIZE_TOKEN_CLIENT);
        Object obj = redisUtil.get(token);
        if(obj!=null){
            u = JacksonUtils.string2Obj(JacksonUtils.obj2String(obj), Member.class);
        }
        return u;
    }
}
